//
//  SYS_ShipmentsCell.m
//  System_App
//
//  Created by 吴健 on 2017/12/5.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "SYS_ShipmentsCell.h"
#import "SYS_SelectGoodsCell.h"

@interface SYS_ShipmentsCell ()<UITableViewDelegate, UITableViewDataSource, DSSelectGoodsCellDelegate, UITextFieldDelegate>

@property (strong, nonatomic) NSMutableArray *allDataArray;

@end

@implementation SYS_ShipmentsCell

+ (SYS_ShipmentsCell *)getShipmentsCell{
    SYS_ShipmentsCell *cell = nil;
    NSArray *objecs = [[NSBundle mainBundle] loadNibNamed:@"SYS_ShipmentsCell" owner:self options:nil];
    for (NSObject *obj in objecs) {
        if ([obj isKindOfClass:[SYS_ShipmentsCell class]]) {
            cell = (SYS_ShipmentsCell *)obj;
            break;
        }
    }
    return cell;
}

// iOS 11的 margins
- (UIEdgeInsets)layoutMargins {
    [super layoutMargins];
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setUI];
}

#pragma mark -- setter and getter
- (void)setUI{
    _shipmentModel = [SYS_ShipmentModel new];
    _mainTableView = [[SYS_TableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 125) style:UITableViewStylePlain];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _mainTableView.scrollEnabled=NO;
    [_headView addSubview:_mainTableView];
    
    [_mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_headView.mas_left);
        make.right.equalTo(_headView.mas_right);
        make.top.equalTo(_headView.mas_top);
        make.bottom.equalTo(_headView.mas_bottom);
    }];
    
    _phoneTextField.delegate = self;
    _numberTextField.delegate = self;
    _companyTextField.delegate = self;
}

- (void)setIndex:(NSIndexPath *)index{
    _index = index;
}

- (void)setShipmentModel:(SYS_ShipmentModel *)shipmentModel{
    _shipmentModel = shipmentModel;
    [_mainTableView reloadData];
    UList *listModel = _shipmentModel.uList[_index.section];
    if (listModel.addressList.count) {
        AddressList *addressModel = listModel.addressList[0];
        //防止省市区出现空
        NSMutableString *sAddress = [NSMutableString stringWithString:addressModel.provinceName];
        [sAddress appendString:addressModel.cityName];
        [sAddress appendString:addressModel.areaName];
        [sAddress appendString:addressModel.detailLocation];
        _addressLabel.text = sAddress;
        listModel.shipAddressId = addressModel.shipAddressId;
        self.lc_addressLabelHeight.constant = [DSUtility heightOfString:_addressLabel.text font:DSFONT_DETAIL width:SCREENWIDTH-26 maxHeight:999]+1;
        self.lc_addressViewHeight.constant = 40+self.lc_addressLabelHeight.constant+1;
    }
    _lc_headViewHeight.constant = 125*listModel.orderItems.count;
    if (listModel.deliveryWayList.count) {
        DeliveryWayList *wayModel = listModel.deliveryWayList[0];
        self.shipWayLabel.text = wayModel.name;
        listModel.deliveryWayId = wayModel.deliveryWayId;
    }
    [self updateConstraintsIfNeeded];
}


#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 125;
}

#pragma mark -- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    UList *listModel = _shipmentModel.uList[_index.section];
    return listModel.orderItems.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"SYS_SelectGoodsCell";
    SYS_SelectGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [SYS_SelectGoodsCell getSelectGoodsCell];
    }
    UList *listModel = _shipmentModel.uList[_index.section];
    cell.indexPath = indexPath;
    cell.delegate = self;
    cell.model = listModel.orderItems[indexPath.section];
    return cell;
}

#pragma mark - DSSelectGoodsCellDelegate
- (void)choseTerm:(UIButton *)button{
    UList *listModel = _shipmentModel.uList[_index.section];
    OrderItems *model = listModel.orderItems[button.tag];
    model.isSelect = !model.isSelect;
}

#pragma mark -- UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
